package com.iceengine.j3d.container;

/**
 * The ObjectContainer class manages a world that is build of Objects.
 * @author penguin
 */
import java.util.ArrayList;
import com.iceengine.j3d.objects.*;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.opengl.GL11;

import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ObjectContainer {
    private ArrayList<IObject> theObjects;
    private Vector3f theTranslation;
    private Vector3f Rotation;
    int[] fogMode = {GL11.GL_EXP, GL11.GL_EXP2, GL11.GL_LINEAR };

    /**
     * Initializes the Container
     * @param translation The translation of the container.
     */
    public ObjectContainer(Vector3f translation)
    {
        theObjects = new ArrayList<IObject>();
        theTranslation = translation;
        Rotation = new Vector3f(0,0,0);
    }

    /**
     * Adds a object that implements IObject to the container.
     * @param toAdd Object to add.
     */
    public void addObject(IObject toAdd)
    {
        theObjects.add(toAdd);
    }

    /**
     * Draws the container.
     */
    public void drawContainer(Vector3f Translation, Vector3f rotation)
    {
        Vector3f translation = theTranslation;
        Vector3f theRotation = Rotation;

        translation = new Vector3f(theTranslation.x+Translation.x,theTranslation.y+Translation.y,theTranslation.z+Translation.z);
        theRotation = new Vector3f(Rotation.x+rotation.x,Rotation.y+(360.0f - rotation.y),Rotation.z+rotation.z);


        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        for(int i = 0; i < theObjects.size(); i++)
        {
            theObjects.get(i).drawObject(translation, theRotation);
        }
    }

    /**
     * Generates variables for the container.
     */
    private void genObject()
    {
        // What is here todo? :/
    }

    /**
     * Rotate the container in X-Axis
     * @param angle Angle to rotate.
     */
    public void rotateX(float angle)
    {
        Rotation.x = angle;
        genObject();
    }

    /**
     * Rotate the container in Y-Axis
     * @param angle Angle to rotate.
     */
    public void rotateY(float angle)
    {
        Rotation.y = angle;
        genObject();
    }

    /**
     * Rotate the container in Z-Axis
     * @param angle Angle to rotate.
     */
    public void rotateZ(float angle)
    {
        Rotation.z = angle;
        genObject();
    }

    /**
     * Enables lightning in the container.
     */
    public void enableLightning()
    {
        GL11.glEnable(GL11.GL_LIGHTING);
    }

    /**
     * Disables lightning in the container.
     */
    public void disableLightning()
    {
        GL11.glDisable(GL11.GL_LIGHTING);
    }

    /**
     * Sets ambient light in the container.
     * @param ambientLight Lightcolor variables.
     */
    public void setAmbientLight(float[] ambientLight)
    {
        GL11.glDisable(GL11.GL_LIGHT1); // Disable Light1
        ByteBuffer temp = ByteBuffer.allocateDirect(16);
        temp.order(ByteOrder.nativeOrder());
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(ambientLight).flip());
        GL11.glEnable(GL11.GL_LIGHT1); // Enable Light1
    }

    /**
     * Sets diffuselight in container.
     * @param diffuseLight Lightning color.
     * @param lightPosition Position of the diffuselight.
     */
    public void setDiffuseLight(float[] diffuseLight, float[] lightPosition)
    {
        GL11.glDisable(GL11.GL_LIGHT1); // Disable Light1
        ByteBuffer temp = ByteBuffer.allocateDirect(16);
        temp.order(ByteOrder.nativeOrder());
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(diffuseLight).flip());
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());
        GL11.glEnable(GL11.GL_LIGHT1); // Enable Light 1
    }

    /**
     * Enables fog in the container.
     * @param fogColor The color of the fog.
     * @param FogMode Mode to draw the fog.
     * @param start Z-Coordinate where the fog starts.
     * @param end Z-Coordinate where the fog ends.
     * @param density Fog density.
     */
    public void enableFog(float fogColor, int FogMode, float start, float end, float density)
    {
        GL11.glClearColor(0.5f,0.5f,0.5f,1.0f);
        GL11.glFogi(GL11.GL_FOG_MODE, fogMode[FogMode]);
        GL11.glFogf(GL11.GL_FOG_COLOR, fogColor);
        GL11.glFogf(GL11.GL_FOG_DENSITY, density);
        GL11.glHint(GL11.GL_FOG_HINT, GL11.GL_NICEST);
        GL11.glFogf(GL11.GL_FOG_START, start);
        GL11.glFogf(GL11.GL_FOG_END, end);
        GL11.glEnable(GL11.GL_FOG);
    }

    /**
     * Disables fog.
     */
    public void disableFog()
    {
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClearDepth(1.0f);
        GL11.glDisable(GL11.GL_FOG);
    }
}
